<template>
    <div class="add-movie-container">
      <div class="form-card">
        <h2>添加新电影</h2>
        <form @submit.prevent="submitMovie">
          <div class="form-group">
            <label>电影标题</label>
            <input 
              type="text" 
              v-model="movie.title" 
              placeholder="请输入电影标题" 
              required
            >
          </div>
          
          <div class="form-row">
            <div class="form-group">
              <label>导演</label>
              <input 
                type="text" 
                v-model="movie.director" 
                placeholder="请输入导演姓名" 
                required
              >
            </div>
            
            <div class="form-group">
              <label>上映年份</label>
              <input 
                type="number" 
                v-model="movie.year" 
                min="1900" 
                max="2025" 
                placeholder="请输入上映年份" 
                required
              >
            </div>
          </div>
          
          <div class="form-group">
            <label>电影类型</label>
            <div class="genres-container">
              <div 
                v-for="genre in availableGenres" 
                :key="genre" 
                :class="['genre-option', { selected: selectedGenres.includes(genre) }]"
                @click="toggleGenre(genre)"
              >
                {{ genre }}
              </div>
            </div>
            <input 
              type="text" 
              v-model="customGenre" 
              placeholder="或输入其他类型" 
              @keyup.enter="addCustomGenre"
            >
          </div>
          
          <div class="form-group">
            <label>电影简介</label>
            <textarea 
              v-model="movie.description" 
              placeholder="请输入电影简介" 
              rows="5" 
              required
            ></textarea>
          </div>
          
          <div class="form-group">
            <label>海报URL (可选)</label>
            <input 
              type="text" 
              v-model="movie.poster" 
              placeholder="请输入海报图片URL"
            >
          </div>
          
          <div class="form-actions">
            <button type="button" class="btn-cancel" @click="cancel">
              取消
            </button>
            <button type="submit" class="btn-submit" :disabled="loading">
              <Spinner v-if="loading" />
              <span v-else>提交</span>
            </button>
          </div>
        </form>
      </div>
    </div>
  </template>
  
  <script setup lang="ts">
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'
  import { useUserStore } from '../store/user'
  import { addMovie } from '../api'
  import Spinner from '../components/Spinner.vue'
  
  const router = useRouter()
  const userStore = useUserStore()
  const loading = ref(false)
  const customGenre = ref('')
  
  const availableGenres = ['动作', '喜剧', '科幻', '爱情', '恐怖', '剧情', '冒险', '动画']
  const selectedGenres = ref<string[]>([])
  
  const movie = ref({
    title: '',
    director: '',
    year: new Date().getFullYear(),
    description: '',
    genres: [] as string[],
    poster: '',
    userId: 0
  })
  
  const toggleGenre = (genre: string) => {
    if (selectedGenres.value.includes(genre)) {
      selectedGenres.value = selectedGenres.value.filter(g => g !== genre)
    } else {
      selectedGenres.value.push(genre)
    }
  }
  
  const addCustomGenre = () => {
    if (customGenre.value.trim() && !selectedGenres.value.includes(customGenre.value)) {
      selectedGenres.value.push(customGenre.value.trim())
      customGenre.value = ''
    }
  }
  
  const submitMovie = async () => {
    if (!userStore.isLoggedIn || !userStore.userInfo) {
      alert('请先登录')
      return
    }
    
    try {
      loading.value = true
      
      const movieData = {
        ...movie.value,
        userId: userStore.userInfo?.id ?? 0, // 直接访问userInfo的id属性
        genres: selectedGenres.value
      }
      
      const response = await addMovie(movieData)
      
      if (response.data.success) {
        alert('电影添加成功！')
        router.push({ name: 'home' })
      } else {
        alert('添加失败: ' + (response.data.message || '请稍后再试'))
      }
    } catch (error) {
      console.error('Add movie error:', error)
      alert('添加失败，请稍后再试')
    } finally {
      loading.value = false
    }
  }
  
  const cancel = () => {
    router.go(-1)
  }
  </script>
  
  <style scoped lang="scss">
  .add-movie-container {
    display: flex;
    justify-content: center;
    min-height: 100vh;
    padding: 40px 20px;
    background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
  }
  
  .form-card {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
    padding: 40px;
    width: 100%;
    max-width: 800px;
    backdrop-filter: blur(10px);
    animation: slideUp 0.5s ease-out;
  }
  
  h2 {
    text-align: center;
    margin-bottom: 30px;
    color: #2c3e50;
    font-size: 28px;
  }
  
  .form-group {
    margin-bottom: 25px;
    
    label {
      display: block;
      margin-bottom: 10px;
      font-weight: 500;
      color: #555;
      font-size: 18px;
    }
    
    input, textarea {
      width: 100%;
      padding: 14px;
      border: 1px solid #ddd;
      border-radius: 8px;
      font-size: 16px;
      transition: all 0.3s;
      
      &:focus {
        border-color: #3498db;
        outline: none;
        box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
      }
    }
    
    textarea {
      min-height: 150px;
      resize: vertical;
    }
  }
  
  .form-row {
    display: flex;
    gap: 20px;
    margin-bottom: 25px;
    
    .form-group {
      flex: 1;
      margin-bottom: 0;
    }
  }
  
  .genres-container {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 15px;
  }
  
  .genre-option {
    padding: 8px 16px;
    background: #f0f7ff;
    color: #3498db;
    border: 1px solid #3498db;
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.3s;
    
    &:hover {
      background: #e1f0ff;
    }
    
    &.selected {
      background: #3498db;
      color: white;
    }
  }
  
  .form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 15px;
    margin-top: 30px;
  }
  
  .btn-cancel, .btn-submit {
    padding: 12px 30px;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s;
  }
  
  .btn-cancel {
    background: #f5f5f5;
    color: #555;
    
    &:hover {
      background: #e0e0e0;
    }
  }
  
  .btn-submit {
    background: linear-gradient(to right, #27ae60, #219653);
    color: white;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
    }
    
    &:disabled {
      background: #bdc3c7;
      cursor: not-allowed;
      transform: none;
      box-shadow: none;
    }
  }
  
  @keyframes slideUp {
    from { opacity: 0; transform: translateY(30px); }
    to { opacity: 1; transform: translateY(0); }
  }
  
  @media (max-width: 768px) {
    .form-row {
      flex-direction: column;
      gap: 25px;
    }
  }
  
  @media (max-width: 576px) {
    .form-card {
      padding: 25px;
    }
    
    h2 {
      font-size: 24px;
    }
  }
  </style>